<?php

/**
 * This is the model class for table "ham_campaign".
 *
 * The followings are the available columns in table 'ham_campaign':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $utm_campaign
 * @property string $utm_medium
 * @property string $utm_source
 * @property string $utm_term
 * @property string $utm_content
 * @property string $created_at
 * @property string $updated_at
 * @property integer $created_by
 * @property integer $ham_campaign_statistic_id
 * @property integer $template_id
 *
 * The followings are the available model relations:
 * @property User $createdBy
 * @property CampaignSchedule[] $campaignSchedules
 * @property CampaignStatistic[] $campaignStatistics
 * @property ContactCampaign[] $contactCampaigns
 */
class Campaign extends CActiveRecord
{
	public $id;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Campaign the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ham_campaign';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, created_at, created_by, ham_campaign_statistic_id, template_id', 'required'),
			array('created_by, ham_campaign_statistic_id, template_id', 'numerical', 'integerOnly'=>true),
			array('name, utm_campaign, utm_medium, utm_source, utm_term, utm_content', 'length', 'max'=>255),
			array('description, updated_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, description, utm_campaign, utm_medium, utm_source, utm_term, utm_content, created_at, updated_at, created_by, ham_campaign_statistic_id, template_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),
			'campaignSchedules' => array(self::HAS_MANY, 'CampaignSchedule', 'campaign_id'),
			'campaignStatistics' => array(self::HAS_MANY, 'CampaignStatistic', 'campaign_id'),
			'contactCampaigns' => array(self::HAS_MANY, 'ContactCampaign', 'campaign_id'),
		);
	}

	public function beforeSave(){
		if($this->isNewRecord){
			$this->created_at = date('Y-m-d H:i');
		}else{
			$this->updated_at = date('Y-m-d H:i');
		}
		return parent::beforeSave();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'description' => 'Description',
			'utm_campaign' => 'Utm Campaign',
			'utm_medium' => 'Utm Medium',
			'utm_source' => 'Utm Source',
			'utm_term' => 'Utm Term',
			'utm_content' => 'Utm Content',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
			'created_by' => 'Created By',
			'ham_campaign_statistic_id' => 'Ham Campaign Statistic',
			'template_id' => 'Template',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('utm_campaign',$this->utm_campaign,true);
		$criteria->compare('utm_medium',$this->utm_medium,true);
		$criteria->compare('utm_source',$this->utm_source,true);
		$criteria->compare('utm_term',$this->utm_term,true);
		$criteria->compare('utm_content',$this->utm_content,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('ham_campaign_statistic_id',$this->ham_campaign_statistic_id);
		$criteria->compare('template_id',$this->template_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Function to process campaign step one
	 */
	public function updateCampaignStepOne(){
		$result = false;
		if($this->isNewRecord){
			$this->step = 1;
			$this->created_by = Yii::app()->user->getId();
			$result = $this->insert();
		}else{

		}
		return $result;
	}

	public function addGroups($group, $campaign_id){
		$contacts = array();
		foreach($group->groupContacts as $c){
			$contact = Contact::model()->findByPk($c->contact_id);
			if(!is_object($contact)) continue;
			$cond_contactCampaign = new CDbCriteria();
			$cond_contactCampaign->compare('contact_id',$contact->id, false,'OR');
			$cond_contactCampaign->compare('email', $contact->email, false,'OR');
			$contactCampaign = ContactCampaign::model()->find($cond_contactCampaign);
			if(is_object($contactCampaign)) continue;
			$contactCampaign = new ContactCampaign;
			$contactCampaign->contact_id = $c->contact_id;
			$contactCampaign->group = $c->group_id;
			$contactCampaign->campaign_id = $campaign_id;
			$contactCampaign->email = $contact->email;
			$contactCampaign->insert();
		}
	}
}